package com.example.electronicmall.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.electronicmall.R;
import com.example.electronicmall.bean.ShoppingCartBean;

import java.util.List;

/*
* 作者：王辉
* 功能：购物车适配器
* 时间：2022/11/2
* */
public class ShoppingCartAdapter extends BaseAdapter {
    private Context context;
    private List<ShoppingCartBean> shoppingCartBeanList;

    private CheckInterface checkInterface;
    private ModifyCountInterface modifyCountInterface;



    public ShoppingCartAdapter(Context context , List<ShoppingCartBean> shoppingCartBeanList){
        this.context=context;
        this.shoppingCartBeanList=shoppingCartBeanList;

    }
    public void setCheckInterface(CheckInterface checkInterface) {
        this.checkInterface = checkInterface;
    }

    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
    }

    @Override
    public int getCount() {
        return shoppingCartBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return shoppingCartBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView==null){
            convertView=View.inflate(context,R.layout.shopping_list_view,null);
            holder=new ViewHolder();
            holder.shopstitle=convertView.findViewById(R.id.shopping_list_shops_name);
            holder.shopsicon=convertView.findViewById(R.id.shopping_list_shops_icon);
            holder.shopsname=convertView.findViewById(R.id.shopping_list_commod_name);
            holder.shopspec=convertView.findViewById(R.id.shopping_list_commod_spec);
            holder.shopsmoney=convertView.findViewById(R.id.shopping_list_money);
            holder.shopscoupon=convertView.findViewById(R.id.shopping_list_coupon);
            holder.checkone=convertView.findViewById(R.id.shopping_choice);
            holder.shopsadd=convertView.findViewById(R.id.shopping_list_add);
            holder.shopsrule=convertView.findViewById(R.id.shopping_list_rule);
            holder.shopsnumber=convertView.findViewById(R.id.shopping_list_number);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }


        ShoppingCartBean info=shoppingCartBeanList.get(position);
        if (info!=null){
            holder.checkone.setChecked(info.isChoosed());
            holder.checkone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    info.setChoosed(((CheckBox)v).isChecked());
                    holder.checkone.setChecked(((CheckBox) v).isChecked());
                    checkInterface.checkChild(position,((CheckBox) v).isChecked());// 暴露子选接口
                }
            });

            holder.shopstitle.setText(shoppingCartBeanList.get(position).getShopstitle());
            holder.shopsicon.setImageBitmap(shoppingCartBeanList.get(position).getShopsicon());
            holder.shopsname.setText(shoppingCartBeanList.get(position).getShopsname());
            holder.shopsmoney.setText(shoppingCartBeanList.get(position).getShopsmoney());
            holder.shopspec.setText(shoppingCartBeanList.get(position).getShopsspec());
            holder.shopsnumber.setText(shoppingCartBeanList.get(position).getShopsnumber());

            holder.shopsadd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    modifyCountInterface.doIncrease(position, holder.shopsnumber, holder.checkone.isChecked());// 暴露增加接口

                }
            });

            holder.shopsrule.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    modifyCountInterface.doDecrease(position, holder.shopsnumber, holder.checkone.isChecked());// 暴露删减接口
                }
            });
        }
        return convertView;
    }


    class  ViewHolder{
        TextView shopstitle,shopsname,shopspec,shopsmoney,shopscoupon,shopsnumber;
        ImageView shopsicon,shopsadd,shopsrule;
        CheckBox  checkone;
    }
    //复选框接口
    public interface CheckInterface{

        public void checkChild(int childPosition, boolean isChecked);
    }
    //改变数量接口
    public interface  ModifyCountInterface{
        //增加操作
        public void doIncrease(int childPosition, View showCountView, boolean isChecked);
        //删减操作
        public void doDecrease(int childPosition, View showCountView, boolean isChecked);
    }
}
